﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using System.Collections.ObjectModel;

namespace PostIt
{
    public partial class DetailsPage : PhoneApplicationPage
    {
        private ItemViewModel ourItem;

        public DetailsPage()
        {
            InitializeComponent();
        }
        int id;

        // When page is navigated to set data context to selected item in list
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {


            string selectedIndex = "";
            string taskID = "";

            if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
            {
                int index = int.Parse(selectedIndex);

                ourItem = App.ViewModel.Items[index];

                DataContext = ourItem;


            }
            else if (NavigationContext.QueryString.TryGetValue("taskId", out taskID))
            {
                ourItem = new ItemViewModel();

                id = Int32.Parse(taskID);
                var tasksDB = from Tasks s in App.DB.Items
                              where s.TaskId == id
                              select s
                              ;

                // Query the database and load the needed task
                var allTask = new ObservableCollection<Tasks>(tasksDB);
                ourItem.BackColor = allTask[0].backColor;
                ourItem.Detail = allTask[0].Detail;
                ourItem.Title = allTask[0].Title;
                ourItem.tileText = allTask[0].tileText;
                ourItem.CreationDate = allTask[0].CreationDate;
                ourItem.DueDate = allTask[0].DueDate.ToShortDateString();



                DataContext = ourItem;


            }
        }

        //Home buton
        private void ApplicationBarIconButton_Click_1(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }

        //edit button
        private void ApplicationBarIconButton_Click_2(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/AddTasks.xaml?taskId=" + id, UriKind.Relative));
        }
    }
}